package spotGUI.ElementViewer;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import spotGUI.GUIElements.MarkerGUI.VoteDisplay;
import utils.PopupUtils;
import elements.Vote;
import java.util.ResourceBundle;

public class MarkableDecoratorView extends JPanel {
	
	
	private Box horizontalBox;
	private JButton btnShowMarks;
	private JPopupMenu popupMenu = null;
	private Component horizontalStrut;
	private Component horizontalGlue;


	
	public MarkableDecoratorView (JPanel markableElement, final ArrayList<Vote> votes) {
		setMaximumSize(new Dimension(510, 2147483647));
		setMinimumSize(new Dimension(500, 10));
	
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{500, 0};
		gridBagLayout.rowHeights = new int[]{16, 27, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		
		GridBagConstraints gbc_element = new GridBagConstraints();
		gbc_element.fill = GridBagConstraints.BOTH;
		gbc_element.insets = new Insets(0, 0, 5, 0);
		gbc_element.gridx = 0;
		gbc_element.gridy = 0;
		add(markableElement, gbc_element);
		
		horizontalBox = Box.createHorizontalBox();
		GridBagConstraints gbc_horizontalBox = new GridBagConstraints();
		gbc_horizontalBox.anchor = GridBagConstraints.NORTH;
		gbc_horizontalBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_horizontalBox.gridx = 0;
		gbc_horizontalBox.gridy = 1;
		add(horizontalBox, gbc_horizontalBox);
		
		btnShowMarks = new JButton(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("MarkableDecoratorView.btnShowMarks.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnShowMarks.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (popupMenu == null) {
					showVotesPopup(votes);
				}
				popupMenu.show(btnShowMarks, btnShowMarks.getMousePosition(true).x, btnShowMarks.getMousePosition(true).y);
			}
		});
		
		horizontalGlue = Box.createHorizontalGlue();
		horizontalBox.add(horizontalGlue);
		horizontalBox.add(btnShowMarks);
		
		horizontalStrut = Box.createHorizontalStrut(20);
		horizontalBox.add(horizontalStrut);
		VoteDisplay voteDisplay = new VoteDisplay(calculateAverage(votes));
		horizontalBox.add(voteDisplay);
	}
	
	private void showVotesPopup(ArrayList<Vote> votes) {
		
		JPanel panel = new JPanel();
		JScrollPane scrollPane = new JScrollPane();
		
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
	
		scrollPane.setViewportView(panel);
		BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
		panel.setLayout(boxLayout);
		
		if (votes.size() == 0) {
			panel.add(new JLabel(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("MarkableDecoratorView.1")));
		}
		
		for (int i = 0; i < votes.size(); i++) {
			panel.add(new VoteView (votes.get(i)));
			}
		
			popupMenu = new JPopupMenu();
			popupMenu.add(scrollPane);
			PopupUtils.addPopup(btnShowMarks, popupMenu);
	}

	private float calculateAverage (ArrayList<Vote> votes) {
		float average = 0;
		for (int i = 0; i < votes.size(); i++) {
			average += votes.get(i).vote;
		}
		average /= votes.size();
		return average;
	}
}
